package com.atguigu.eduservice.controller;


import com.atguigu.comMonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;

import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.exceptionHandler.DuplicatedNameException;
import com.atguigu.exceptionHandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2024-03-27
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
@Slf4j
public class EduTeacherController {
    @Autowired
    private EduTeacherService teacherService;


    @GetMapping("findAll")
    @ApiOperation(value = "查询所有讲师")
    //    1.查询所有讲师数据.
    public R getAllTeacher() {
        //调用service的方法实现查询所有的操作
        List<EduTeacher> list = teacherService.list(null);

        return R.ok().data("items", list);
    }

    @ApiOperation(value = "根据id删除讲师")
    @ApiParam(name = "id", value = "讲师ID", required = true)
    @DeleteMapping("{id}")
    public R deleteTeacherById(@PathVariable String id) {
        boolean b = teacherService.removeById(id);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    @GetMapping("page/{current}/{limit}")
    @ApiOperation(value = "分页查询讲师")
    public R pageListTeacher(@PathVariable long current, @PathVariable long limit) {
        Page<EduTeacher> pageTeacher = new Page<EduTeacher>(current, limit);
        IPage<EduTeacher> page = teacherService.page(pageTeacher, null);
//        查询到的数据都放在pageTeacher中.
        return R.ok().data("rows", pageTeacher.getRecords()).data("total", page.getTotal());
    }

    @PostMapping("pageCondition/{current}/{limit}")
    @ApiOperation(value = "分页多条件查询讲师")
//    TeacherQuery 有name   level  begin  end
    public R pageListTeacherCondition(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) TeacherQuery teacherQuery) {
        log.info("teacherQuery:{}", teacherQuery);
        //        page
        Page<EduTeacher> pageTeacher = new Page<EduTeacher>(current, limit);
        //        wrap
        LambdaQueryWrapper<EduTeacher> wrapper = new LambdaQueryWrapper<>();

        /**
         * 根据名字和等级  开始   结束时间作为条件
         */
        wrapper.like(teacherQuery.getName() != null, EduTeacher::getName, teacherQuery.getName());
        wrapper.eq(teacherQuery.getLevel() != null, EduTeacher::getLevel, teacherQuery.getLevel());
        wrapper.ge(teacherQuery.getBegin() != null, EduTeacher::getGmtCreate, teacherQuery.getBegin());
        wrapper.le(teacherQuery.getEnd() != null, EduTeacher::getGmtModified, teacherQuery.getEnd());
        wrapper.orderByDesc(EduTeacher::getSort);
        teacherService.page(pageTeacher, wrapper);
        return R.ok().data("rows", pageTeacher.getRecords()).data("total", pageTeacher.getTotal());
    }


    @PostMapping("addTeacher")
    @ApiOperation(value = "添加讲师")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        String name = eduTeacher.getName();
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);

//        EduTeacher one = teacherService.getOne(wrapper);
//        if (one == null) {

            boolean save = teacherService.save(eduTeacher);
            if (save) {
                return R.ok();
            }else {
                return R.error();
            }

//        } else {
//            throw new DuplicatedNameException(20001, "讲师名字已存在");
//        }

    }

    //    数据回显
    @GetMapping("getTeacher/{id}")
    @ApiOperation(value = "根据id查询讲师")
    public R selectTeacherById(@PathVariable String id) {
        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("teacher", eduTeacher);
    }


    @PostMapping("updateTeacher")
    @ApiOperation(value = "更新讲师信息")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean b = teacherService.updateById(eduTeacher);
        if (b) {
            return R.ok();
        } else {
            return R.error();

        }
    }
}

